<?php

namespace App\Api\Controllers;

use App\Api\Requests\UserAddressRequest;
use App\User;
use App\Models\UserAddress;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class UserAddressesController extends Controller
{
    //返回用户收货地址列表
    public function index()
    {
        $user = auth()->user();
        $address = UserAddress::where('user_id', $user['id'])
            ->orderBy('id', 'desc')
            ->get();
        foreach ($address as &$v) {
            $v->full_address = $v->FullAddress;
        }
        if($address){
            $address = $address->toArray();
        }
        return $this->responseData($address);
    }

    //收货地址详情
    public function show(Request $request)
    {
        if (!$request->id) {
            return $this->responseFailed('ID必传');
        }
        $addr =  UserAddress::find($request->id);
        if(!$addr){
            return $this->responseFailed('无此收货地址');
        }
        return $this->responseData($addr->toArray());
    }

    //添加收货地址接口
    public function store(UserAddressRequest $request)
    {
        $user = auth()->user();
        if ($request->input('is_default') == 1) {
            $addr = UserAddress::where('user_id',$user['id'])
                ->where('is_default', '=', '1')
                ->orderBy('id','asc')
                ->first();
            if ($addr) {
                $addr->is_default = 0;
                $addr->save();
            }
        }
        $user_address = new UserAddress();
        $user_address->fill($request->all());
        $user_address->user_id = $user['id'];
        $user_address->save();
        return $this->responseData($user_address->toArray());
    }

    //修改收货地址接口
    public function update(UserAddressRequest $request)
    {
        if (!$request->id) {
            return $this->responseFailed('地址ID必传');
        }
        DB::beginTransaction();
        $user_address = UserAddress::find($request->id);
        $user = auth()->user();
        if ($request->input('is_default') == 1) {
            $addr = UserAddress::where('user_id',$user['id'])
                ->where('is_default', '=', '1')
                ->orderBy('id','asc')
                ->first();
            if ($addr) {
              if ($addr['id'] != $request->id) {
                  $addr->is_default = 0;
                  $addr->save();
              }
            }
        }
        $input = $request->only([
            'province',
            'city',
            'district',
            'address',
            'contact_name',
            'contact_phone',
            'is_default',
        ]);
        $row = $user_address->update($input);
        if ($row) {
            DB::commit();
            return $this->responseData($user_address->toArray());
        } else {
            DB::rollBack();
            return $this->responseFailed();
        }
    }

    //设置默认地址
    public function setDefault(Request $request)
    {
        $id = $request->input('id');
        $user = auth()->user();
        $addr = UserAddress::where('user_id',$user['id'])
            ->where('is_default', '=', '1')
            ->orderBy('id','asc')
            ->first();
        if ($addr) {
            $addr->is_default = 0;
            $addr->save();
        }
        $addr = UserAddress::find($id);
        $addr->is_default = 1;
        if ($addr->save()) {
            return $this->responseSuccess();
        }
        return $this->responseFailed();
    }

    //删除收货地址
    public function destroy(Request $request)
    {
        if (!$request->id) {
            return $this->responseFailed('地址ID必传');
        }
        $user_address = UserAddress::find($request->id);
        if (!$user_address) {
            return $this->responseFailed('地址不存在');
        }
        if ($user_address->delete()){
            return $this->responseSuccess();
        } else {
            return $this->responseFailed();
        }
    }
}
